import { createWebHistory, createRouter } from "vue-router";
import HomeView from "@/view/HomeView.vue";
const routes = [
  {
    //首页
    path: "/",
    name: "home",
    component: HomeView,
  },
      //文章
  {
    path: "/article",
    name: "article",
    component: () => import("@/view/ArticleView.vue"),
  },
  //文章详情
  {
    path: "/article/:id",
    name: "articleDetail",
    component: () => import("@/view/detail/ArticleDetailView.vue"),
  },
  //功能
  {
     //功能详情页面
    path: "/function/:id",
    name: "function",
    component: () => import("@/view/function/index.vue"),
  },
  {
    //图库
    path: "/gallery",
    name: "gallery",
    component: () => import("@/view/GalleryView.vue"),
  },
  {
    //图库详情
    path: "/gallery/:id",
    name: "galleryDetail",
    component: () => import("@/view/detail/GalleryDetailView.vue"),
  },

  {
    //随笔
    path: "/diary",
    name: "diary",
    component: () => import("@/view/DiaryView.vue"),
  },
  {
    //关于我
    path: "/about",
    name: "about",
    component: () => import("@/view/AboutView.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

//在路由请求前进行的拦截
router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {
    //如果地址错误或未匹配到路由
    from.name ? next({ name: from.name }) : next("/errorinfo");
  } //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
  else {
    next();
  }
});

export default router;
